/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.ui.activities;

import java.util.ArrayList;
import java.util.Calendar;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import utsc.apps.utschedule.R;
import utsc.apps.utschedule.services.TimetableBroadcastReceiver;
import utsc.apps.utschedule.services.TimetableDataService;
import utsc.apps.utschedule.ui.ScheduleFragment;
import utsc.apps.utschedule.ui.StockSettingFragment;
import utsc.apps.utschedule.util.TimetableHelper;
import utsc.apps.utschedule.widget.TimerWeekHeaderLayout.OnDaySelectedListener;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.NavUtils;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.SimpleOnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

/**
 * This is the main activity for the timetable feature.
 */
public class TimetableMainActivity extends GenericFragmentActivity implements OnDaySelectedListener{

	private ActionBar _actionbar;
	public static final int MODE_DAYVIEW=0;
	public static final int MODE_WEEKVIEW=1;
	private static int _mode=MODE_WEEKVIEW;
	private ViewPager _pager;
	private SchedulePagerAdapter _pageAdapter;
	private static int _focusedPage=1;
	private static boolean _weekViewDefault=true;
	private TimetableActionSpinnerAdapter _navigationListAdapter;
	private boolean _start=true;
	
	private TimetableBroadcastReceiver _receiver=new TimetableBroadcastReceiver(){
		public void onReceive(android.content.Context context, Intent arg1) {
			_pageAdapter.refreshPages();
		};
	};
	
	@Override
	public void onPause() {
		super.onPause();
		 LocalBroadcastManager.getInstance(this).unregisterReceiver(_receiver);
	}	
	
	@Override
	protected void onResume() {
		super.onResume();
		IntentFilter iff= new IntentFilter(TimetableDataService.ACTION_COURSES);
		LocalBroadcastManager.getInstance(this).registerReceiver(_receiver, iff);
		
		//on resume, we always setup the view pages
		//if the app is not during starting
		if (!_start) {
			_pageAdapter.setupPages(this);
			_pageAdapter.notifyDataSetChanged();
		}
		//this is important!!
		//we always set the initial page to our focused page
		//even during startup before we set anything
		_pager.setCurrentItem(_focusedPage,false);

	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		if (savedInstanceState!=null) {
			_start=savedInstanceState.getBoolean("started");
		}
	}
		
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		if (hasFocus && _start) {
			if (_start) {
				_pageAdapter.setupPages(this);
				_pageAdapter.notifyDataSetChanged();
				_pager.setCurrentItem(_focusedPage,false);
				_start=false;
			}
		}
	}

	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		
		if (arg0!=null) {
			_start=arg0.getBoolean("started");
		}
		
		setContentView(R.layout.activity_timetable);
		_actionbar=getSupportActionBar();
		_actionbar.setDisplayShowTitleEnabled(false);
		_actionbar.setDisplayHomeAsUpEnabled(true);
		_actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		
		_navigationListAdapter=new TimetableActionSpinnerAdapter(this, getResources().getTextArray(R.array.array_timetable_actionlist));
		_actionbar.setListNavigationCallbacks(_navigationListAdapter, _onNavigationListener);
		setupResources();
		_actionbar.setSelectedNavigationItem(_mode);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		case R.id.today:
			int today=TimetableHelper.getDayOfYear();
			int value=today;
			int calendarField=Calendar.DAY_OF_YEAR;
			if (_mode==MODE_WEEKVIEW) {
				value=TimetableHelper.getCurrentWeekNumber();
				calendarField=Calendar.WEEK_OF_YEAR;
			}
			((ScheduleFragment)_pageAdapter.getPage(0)).setWeekOrDayOfYear(value-1, calendarField);
			((ScheduleFragment)_pageAdapter.getPage(1)).setWeekOrDayOfYear(value,  calendarField);
			((ScheduleFragment)_pageAdapter.getPage(2)).setWeekOrDayOfYear(value+1,  calendarField);
			_navigationListAdapter.notifyDateChanged(value,  calendarField);
			SchedulePagerAdapter.set_currentDayOfYear(today);
			return true;
		case R.id.setting:
			Intent settingIntent=null;
			if (Build.VERSION.SDK_INT<11) {
				settingIntent=new Intent(this, SettingActivity.class);
			}else {
				settingIntent=new Intent(this, StockSettingActivity.class);
				settingIntent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, StockSettingFragment.class.getName());
				settingIntent.putExtra(PreferenceActivity.EXTRA_NO_HEADERS, true);
			}
			
			startActivity(settingIntent);
			return true;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getSupportMenuInflater().inflate(R.menu.today_menu, menu);
		return true;
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		//this line has to be called before the parent method call
		//to make sure the view pages are removed from stack
		_pageAdapter.removePages();
		outState.putBoolean("started", _start);
		super.onSaveInstanceState(outState);
	}
	
	private SimpleOnPageChangeListener _pageChangeListener=new SimpleOnPageChangeListener(){
		
		@Override
		public void onPageSelected(int position) {
			//always refresh the focused page
			_focusedPage=position;
		}
		
		private void swipeView(int mode) {
			boolean isDayMode=mode==TimetableMainActivity.MODE_DAYVIEW;
			//get all the 3 pages: left, centre , and right
			final ScheduleFragment fLeft = ((ScheduleFragment) _pageAdapter
					.getPage(0));
			final ScheduleFragment fCenter = ((ScheduleFragment) _pageAdapter
					.getPage(1));
			final ScheduleFragment fRight = ((ScheduleFragment) _pageAdapter
					.getPage(2));
			//cache the new day of year number with the centre one
			int newValue=(isDayMode?fCenter.get_dayOfYear():fCenter.get_weekNum());
			
			//we only perform the swipe action when we are not in the centre
			if (_focusedPage != 1) {
				int numLeft=0;
				int numCenter=0;
				int numRight = 0;
				int calendarSetFieldType=0;
				if (isDayMode) {
					numLeft = fLeft.get_dayOfYear();
					numCenter = fCenter.get_dayOfYear();
					numRight = fRight.get_dayOfYear();
					calendarSetFieldType=Calendar.DAY_OF_YEAR;
					
				}else {
					numLeft = fLeft.get_weekNum();
					numCenter = fCenter.get_weekNum();
					numRight = fRight.get_weekNum();
					calendarSetFieldType=Calendar.WEEK_OF_YEAR;
				}
				if (_focusedPage == 0) {
					newValue = numLeft - 1;
					fLeft.setWeekOrDayOfYear(newValue, calendarSetFieldType);
					fCenter.setWeekOrDayOfYear(numLeft,calendarSetFieldType);
					fRight.setWeekOrDayOfYear(numCenter,calendarSetFieldType);

				} else if (_focusedPage == 2) {
					newValue = numRight + 1;
					fLeft.setWeekOrDayOfYear(numCenter, calendarSetFieldType);
					fCenter.setWeekOrDayOfYear(numRight, calendarSetFieldType);
					fRight.setWeekOrDayOfYear(newValue, calendarSetFieldType);
				}
									
				// always set to middle page to continue to be able to
				// scroll up/down
				_pager.setCurrentItem(1,false);
			}
			if (_focusedPage==1) {
				//update the global static fields in both the action bar list and the view pager
				if (isDayMode) {
					final int centerDayNum=fCenter.get_dayOfYear();
					_navigationListAdapter.notifyDateChanged(centerDayNum,Calendar.DAY_OF_YEAR);
					SchedulePagerAdapter.set_currentDayOfYear(centerDayNum);
				}else {
					final int centerWeekNum=fCenter.get_weekNum();
					_navigationListAdapter.notifyDateChanged(centerWeekNum,Calendar.WEEK_OF_YEAR);
					SchedulePagerAdapter.set_currentWeekNum(centerWeekNum);
				}
				invalidateOptionsMenu();
			}
		}
		
		@Override
		public void onPageScrollStateChanged(int state) {
			//change the current page back to centre when the state is idle
			if (state == ViewPager.SCROLL_STATE_IDLE) {
				
				swipeView(_mode);
			}
			//update and refresh the next page when user is dragging
			else if (state==ViewPager.SCROLL_STATE_DRAGGING) {
				final ScheduleFragment fLeft = ((ScheduleFragment) _pageAdapter
						.getPage(0));
				final ScheduleFragment fCenter = ((ScheduleFragment) _pageAdapter
						.getPage(1));
				final ScheduleFragment fRight = ((ScheduleFragment) _pageAdapter
						.getPage(2));
				final int yScrollPosition=fCenter.get_globalScrollPosition();
					
				fLeft.scrollToY(yScrollPosition);
				fRight.scrollToY(yScrollPosition);
				//only perform the refresh if necessary 
					fLeft.refresh();
					fCenter.refresh();
					fRight.refresh();
			}
		}
		
	};
	
	/**
	 * This is a general method to setup all the necessary resources.
	 */
	private void setupResources(){
		_pager=(ViewPager) findViewById(R.id.pager);
		_pageAdapter=new SchedulePagerAdapter(getSupportFragmentManager());
		_pager.setAdapter(_pageAdapter);
		_pager.setOnPageChangeListener(_pageChangeListener);
		
	}
	
	private OnNavigationListener _onNavigationListener=new OnNavigationListener() {
		
		@Override
		public boolean onNavigationItemSelected(int itemPosition, long itemId) {
			//prevent the redundant refresh during start up
			if (_pager.getChildCount()>0&&!_weekViewDefault) {
				if (itemPosition==0) {
					_mode=MODE_DAYVIEW;
					_pageAdapter.set_mode(TimetableMainActivity.MODE_DAYVIEW,null);
				}else {
					_mode=MODE_WEEKVIEW;
					_pageAdapter.set_mode(TimetableMainActivity.MODE_WEEKVIEW,TimetableMainActivity.this);
				}
				_pageAdapter.notifyDataSetChanged();
			}
			else {
				_weekViewDefault=false;
			}
			return true;
		}
	};
	
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unbindDrawables(findViewById(R.id.pager));
		if (isFinishing()) {
			_pageAdapter.reset();
			_navigationListAdapter.reset();
			_weekViewDefault=true;
			_mode=MODE_WEEKVIEW;
			_start=true;
		}
	}
	
	public static class TimetableActionSpinnerAdapter extends BaseAdapter implements SpinnerAdapter{
		
		private static int _selectedWeek=-99;
		private Context _context;
		private CharSequence[] _charSequences;
		private static int _selectedDayOfYear=-99;
		
		/**
		 * This method will reset all the global field back to their initial value. Make sure you call it before you finish the activity.
		 */
		public void reset() {
			_selectedWeek = -99;
			_selectedDayOfYear = -99;
		}
		
		public TimetableActionSpinnerAdapter(Context context, CharSequence[] charSequences){
			super();
			this._context=context;
			this._charSequences=charSequences;
			
			//initialize all the global fields to the current time/date
			if (_selectedWeek==-99) {
				_selectedWeek=TimetableHelper.getCurrentWeekNumber();
			}
			if (_selectedDayOfYear==-99) {
				_selectedDayOfYear=TimetableHelper.getDayOfYear();
				//_selectedMonth=TimetableHelper.getMonth(_selectedDayOfYear,Calendar.DAY_OF_YEAR);
				//_selectedDay=TimetableHelper.getDay(_selectedDayOfYear,Calendar.DAY_OF_YEAR);
			}
		}
		
		@Override
		public View getDropDownView(int position, View convertView,
				ViewGroup parent) {
			TimetableSpinnerHolder holder;
			String title=(String) getItem(position);
			if (convertView==null) {
				convertView=LayoutInflater.from(_context).inflate(R.layout.spinner_item_timetable_actionbar, null);
				holder=new TimetableSpinnerHolder();
				holder.itemTitle=(TextView) convertView.findViewById(R.id.item_title);
				holder.itemSubtitle=(TextView) convertView.findViewById(R.id.item_subtitle);
				convertView.setTag(holder);
			}else {
				holder=(TimetableSpinnerHolder) convertView.getTag();
			}
			
			if (title.equals("Week")) {
				holder.itemSubtitle.setText(TimetableHelper.getWeekRangeString(_selectedWeek));
			}else {
				holder.itemSubtitle.setText(TimetableHelper.getFormattedString(TimetableHelper.getPredefinedTime(_selectedDayOfYear, Calendar.DAY_OF_YEAR), TimetableHelper.TYPE_DATE));
			}
			
			holder.itemTitle.setText(title);
			return convertView;
		}
		
		/**
		 * This is the view holder for {@link  utsc.apps.utschedule.ui.activities.TimetableMainActivity.TimetableActionSpinnerAdapter}.
		 * @author UTSChedule team
		 *
		 */
		private static class TimetableSpinnerHolder{
			 TextView itemTitle;
			 TextView itemSubtitle;
		}
		
		@Override
		public int getCount() {
			return _charSequences.length;
		}
		
		/**
		 * This method will notify the adapter to update all the views. This also update the underlying global fields.
		 * @param value -the calendar value
		 * @param calendarSetField -the corresponding calendar field
		 */
		public void notifyDateChanged(int value, int calendarSetField) {
			if (calendarSetField==Calendar.WEEK_OF_YEAR) {
				_selectedWeek=value;
				_selectedDayOfYear=TimetableHelper.getDayOfYear(value, calendarSetField);
			}else {
				_selectedDayOfYear=value;
				_selectedWeek=TimetableHelper.getWeekOfYear(value, calendarSetField);
			}
			//Log.d("notifyDateChanged", value+", dayOfYear "+_selectedDayOfYear);
			//_selectedMonth=TimetableHelper.getMonth(value, calendarSetField);
			//_selectedDay=TimetableHelper.getDay(value, calendarSetField);
			notifyDataSetChanged();
		}
		
		@Override
		public Object getItem(int position) {
			return _charSequences[position];
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TimetableSpinnerHolder holder;
			String title=(String) getItem(position);
			if (convertView==null) {
				convertView=LayoutInflater.from(_context).inflate(R.layout.spinner_view_timetable_actionbar, null);
				holder=new TimetableSpinnerHolder();
				holder.itemTitle=(TextView) convertView.findViewById(R.id.item_title);
				holder.itemSubtitle=(TextView) convertView.findViewById(R.id.item_subtitle);
				
				if (title.equals("Week")) {
					holder.itemSubtitle.setVisibility(View.GONE);
				}
				
				convertView.setTag(holder);
			}else {
				holder=(TimetableSpinnerHolder) convertView.getTag();
			}
			if (title.equals("Week")) {
				//Log.d("refresh menu list", _selectedWeek+""+TimetableHelper.getWeekRangeString(_selectedWeek));
				holder.itemTitle.setText(TimetableHelper.getWeekRangeString(_selectedWeek));
			}else {
				holder.itemSubtitle.setText(TimetableHelper.getFormattedString(TimetableHelper.getPredefinedTime(_selectedDayOfYear,Calendar.DAY_OF_YEAR), TimetableHelper.TYPE_WEEKDAY));
				holder.itemTitle.setText(TimetableHelper.getDayString(_selectedDayOfYear));
			}
			return convertView;
		}

	}
	
	public static class SchedulePagerAdapter extends FragmentPagerAdapter {
		private static int _currentWeekNum=-99;
		private ArrayList<Fragment>_pages=new ArrayList<Fragment>(3);
		private static int _currentDayOfYear=-99;
		private static int _mode=TimetableMainActivity.MODE_WEEKVIEW;
		private FragmentManager _fragmentManager;
		private static boolean _start=true;
		private int _countStart=0;
		
		public void reset(){
			_currentWeekNum=-99;
			_currentDayOfYear=-99;
			_mode=TimetableMainActivity.MODE_WEEKVIEW;
			_start=true;
		}
		
		public void setPage(int position,Fragment page,OnDaySelectedListener listener){
			if (_pages!=null&&!_pages.isEmpty()) {
				_pages.set(position, page);
			}
		}
		
		public  void set_mode(int _mode,OnDaySelectedListener listener) {
			SchedulePagerAdapter._mode = _mode;
			setupPages(listener);
		}
		
		public void setupPages(OnDaySelectedListener listener){
			
			if (_mode==TimetableMainActivity.MODE_DAYVIEW) {
				if (!_pages.isEmpty()) {
					_pages.clear();
				}
				if (_currentDayOfYear==-99) {
					_currentDayOfYear=TimetableHelper.getDayOfYear();;
				}
				_pages.add(ScheduleFragment.newInstance(_currentDayOfYear-1));
				_pages.add(ScheduleFragment.newInstance(_currentDayOfYear));
				_pages.add(ScheduleFragment.newInstance(_currentDayOfYear+1));
				
			}else {
				//Log.d("pager", "setupWeekviews");
				if (!_pages.isEmpty()) {
					_pages.clear();
				}
				if (_currentWeekNum==-99) {
					_currentWeekNum=TimetableHelper.getCurrentWeekNumber();//_currentDayOfYear=TimetableHelper.getDayOfYear();;
				}
				ScheduleFragment f1=ScheduleFragment.newInstance(_currentWeekNum-1,false);
				f1.setOnWeekHeaderDaySelectedListener(listener);
				ScheduleFragment f2=ScheduleFragment.newInstance(_currentWeekNum,false);
				f2.setOnWeekHeaderDaySelectedListener(listener);
				ScheduleFragment f3=ScheduleFragment.newInstance(_currentWeekNum+1,false);
				f3.setOnWeekHeaderDaySelectedListener(listener);
				_pages.add(f1);
				_pages.add(f2);
				_pages.add(f3);
			}
		}
		
		
		public static void set_currentWeekNum(int _currentWeekNum) {
			SchedulePagerAdapter._currentWeekNum = _currentWeekNum;
			_currentDayOfYear=TimetableHelper.getDayOfYear(_currentWeekNum, Calendar.WEEK_OF_YEAR);
		}
		
		public static void set_currentDayOfYear(int _currentDayOfYear) {
			SchedulePagerAdapter._currentDayOfYear = _currentDayOfYear;
			_currentWeekNum=TimetableHelper.getWeekOfYear(_currentDayOfYear, Calendar.DAY_OF_YEAR);
		}
		
		/**
		 *IMPORTANT! note that the constructor shouldn't initialize the underlying fragment list
		 *, call {@code setupPages()} in the {@code Activity.onResume} method instead
		 * @param fm
		 */
		public SchedulePagerAdapter(FragmentManager fm) {
			super(fm);
			_fragmentManager=fm;
		}
		
		public Fragment getPage(int position){
			return _pages.get(position);
		}
		
		@Override
		public Fragment getItem(int position) {
			//Log.d("getItem Start", _start+"");
			return _pages.get(position);//addPage(position);
		}
		
		public Fragment addPage(int position) {
			Fragment fragment=ScheduleFragment.newInstance(_currentWeekNum+position-1, false);
			if (_pages.size()!=position&&_pages.get(position) != null) {
				 _pages.set(position, fragment);
			}else {
				_pages.add(fragment);
			}
			
			 return fragment;
		}

		@Override
		public int getCount() {
			//always return 3
			return 3;
		}
		
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			//IMPORTANT!! Always return null
			//here is the trick to prevent the viewpager 
			//to start the layout before we set the initial page to 1
			if (_start) {
				return null;
			}
			Object item=super.instantiateItem(container, position);
			//Log.d("viewPager", "added page "+((Fragment)item).getTag());
			//IMPORTANT!! we always cache the page back to the fragment list for manipulation
			_pages.set(position, (Fragment) item);
			return item;
		}
		
		@Override
		public int getItemPosition(Object object) {
			//always return POSITION_NONE, so the call to "notifyDataSetChanged" will always get called
			return POSITION_NONE;
		}
		
		@Override
		public void destroyItem(ViewGroup pager, int position, Object view) {
			//IMPORTANT!! 
			//here is the trick to prevent the viewpager 
			//to start the layout before we set the initial page to 1
			if (_start) {
				_countStart++;
				if (_countStart==3) {
					_start=false;
				}
				return;
			}
			String viewID=((Fragment) view).getTag();
			//Log.d("viewPager", "destroyed page "+((Fragment)view).getTag());
			super.destroyItem(pager, position, view);
			
			//IMPORTANT!! we always delete the fragment page from the stack when is it destroyed
			//to make sure the switch between different timetable mode will always work 
			FragmentTransaction ft=_fragmentManager.beginTransaction();
			if (_fragmentManager.findFragmentByTag(viewID) != null) {
				if (position>0) {
					//if (viewID>0) {
						ft.replace(pager.getId(),_pages.get(position),viewID);
						ft.commit();
					//}
				}
			}
		}
		
		/**
		 * A helper method to remove all the underlying fragment pages.
		 */
		public void removePages(){
			/*FragmentTransaction ft=_fragmentManager.beginTransaction();
			ft.remove(_pages.get(0));
			ft.remove(_pages.get(1));
			ft.remove(_pages.get(2));
			ft.commit();*/
		}
		
		/**
		 * This method will notify the adapter to update all the views. This also update the underlying global fields.
		 * @param value -the calendar value
		 * @param calendarSetField -the corresponding calendar field
		 */
		public void notifyDateChanged(int calendarSetField, int value) {
			if (calendarSetField==Calendar.WEEK_OF_YEAR) {
				_currentWeekNum=value;
				_currentDayOfYear=TimetableHelper.getDayOfYear(value, calendarSetField);
			}else {
				_currentDayOfYear=value;
				_currentWeekNum=TimetableHelper.getWeekOfYear(value, calendarSetField);
			}
		}
		
		public void refreshPages() {
			ScheduleFragment f1=((ScheduleFragment)_pages.get(0));
			ScheduleFragment f2=((ScheduleFragment)_pages.get(1));
			ScheduleFragment f3=((ScheduleFragment)_pages.get(2));
			f1.set_refreshView(true);
			f2.set_refreshView(true);
			f3.set_refreshView(true);
			f1.refresh();
			f2.refresh();
			f3.refresh();
			f1.set_refreshView(false);
			f2.set_refreshView(false);
			f3.set_refreshView(false);
		}
	}

	@Override
	public void onDaySelected(int week, int weekday) {
		_mode=MODE_DAYVIEW;
		int day=TimetableHelper.getDayOfYear(week, Calendar.WEEK_OF_YEAR,weekday,Calendar.DAY_OF_WEEK);
		
		_navigationListAdapter.notifyDateChanged(day, Calendar.DAY_OF_YEAR);
		SchedulePagerAdapter.set_currentDayOfYear(day);
		invalidateOptionsMenu();
		_actionbar.setSelectedNavigationItem(0);
	}
	
}
